package kangbo.studentmanagementsystem;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.util.jar.Attributes;

public class Message_gridPane {

    public static GridPane gridPane = new GridPane();

    /********信息*获取*set***********/
    static TextField ID = new TextField();
    static TextField name = new TextField();
    static String gender="";
    static RadioButton radioButton1 = new RadioButton();
    static RadioButton radioButton2 = new RadioButton();
    static TextField phoneNumber = new TextField();
    static ToggleGroup group = new ToggleGroup();
    static TextField birthday_text = new TextField();
    static TextField address_text = new TextField();

    public static TextField getID() {
        return ID;
    }

    public void setID(TextField ID) {
        this.ID = ID;
    }

    public static TextField getName() {
        return name;
    }

    public void setName(TextField name) {
       this.name = name;
    }

    public  void setRadioButton2(RadioButton radioButton2) {
        this.radioButton2 = radioButton2;
    }

    public static TextField getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(TextField phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public static TextField getBirthday_text() {
        return birthday_text;
    }

    public void setBirthday_text(TextField birthday_text) {
        this.birthday_text = birthday_text;
    }

    public static TextField getAddress_text() {
        return address_text;
    }

    public void setAddress_text(TextField address_text) {
        this.address_text = address_text;
    }

    /**设置数据**/
    public static void setMessage(String s){
        String a[]= s.split(",");
        ID.setText(a[0]);
        name.setText(a[1]);
        if(a[2].equals("男")){
            group.selectToggle(radioButton1);
        }
        else if(a[2].equals("女")){
            group.selectToggle(radioButton2);
        }
        gender=a[2];
        birthday_text.setText(a[3]);
        address_text.setText(a[4]);
        phoneNumber.setText(a[5]);
    }


    /**********清除数据*****/
    public static void ClearMessage(){
        ID.setText("");
        name.setText("");
//        set(new TextField(a[0]));//性别
        /******/
        radioButton1.setScaleShape(false);
        radioButton2.setScaleShape(false);
        birthday_text.setText("");
        address_text.setText("");
        phoneNumber.setText("");
    }

    /**设置只读*/
    public static void setReadAndwrite(boolean flag){
        ID.setEditable(flag);
        name.setEditable(flag);
        birthday_text.setEditable(flag);
        address_text.setEditable(flag);
        phoneNumber.setEditable(flag);

        radioButton1.setDisable(!flag);
        radioButton2.setDisable(!flag);
        //需要设置按钮只读
//        radioButton1.setselectedProperty(false);
//        radioButton2.setScaleShape(false);
    }

    /***获取数据*/
    public static String getMassage() {
        String s = "";
        s=getID().getText()+","+getName().getText()+","+gender+","+getBirthday_text().getText()+","
                +getAddress_text().getText()+","+getPhoneNumber().getText();
        return s;
    }

    /**按钮监听器*********/
    public static void RadioButtonmonitor(){
        radioButton1.selectedProperty().addListener(new ChangeListener<Boolean>() {   //r1监听单独

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
//                System.out.println("r1是否被选择：："+newValue);
                if (newValue==true){
                    gender="男";
                }
            }
        });
        radioButton2.selectedProperty().addListener(new ChangeListener<Boolean>() {   //r2听单独

            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
//                System.out.println("r2是否被选择：："+newValue);
                if (newValue==true){
                    gender="女";
                }
            }
        });
    }

    /**girdPane***/
    GridPane getgridPane(){
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setPadding(new Insets(25,25,25,25));
        gridPane.setHgap(13);
        gridPane.setVgap(25);


        Label student_ID = new Label("学号：");
        ID.setPrefColumnCount(10);
        Label student_name= new Label("姓名：");
//        student_name.setPrefWidth(40);
        name.setPrefWidth(10);

        gridPane.add(student_ID,0,0);
        gridPane.add(ID,1,0);
        gridPane.add(student_name,2,0);
        gridPane.add(name,3,0);


        Label gender = new Label("性别：");

        HBox hBox = new HBox(10);
        radioButton1.setText("男");
        radioButton1.setToggleGroup(group);
        radioButton2.setText("女");
        radioButton2.setToggleGroup(group);
        hBox.getChildren().addAll(radioButton1,radioButton2);

        //单选按钮事件处理
        RadioButtonmonitor();


        Label bitrhday = new Label("生日：");
        name.setPrefColumnCount(10);

        gridPane.add(gender,0,1);
        gridPane.add(hBox,1,1);
        gridPane.add(bitrhday,2,1);
        gridPane.add(birthday_text,3,1);

        Label address = new Label("家庭地址：");



        gridPane.add(address,0,2);
        gridPane.add(address_text,1,2,3,1);
//        gridPane.setColumnSpan(address_text, 3);//

        Label phone= new Label("联系电话：");
        address_text.setPrefWidth(40);

        gridPane.add(phone,0,3);
        gridPane.add(phoneNumber,1,3,3,1);


        return gridPane;
    }

    Message_gridPane (){
        getgridPane();
    }
}
